package com.ssm.song.book.service.impl;

import com.ssm.song.book.domain.Card;
import com.ssm.song.book.domain.Reader;
import com.ssm.song.book.domain.vo.ReaderVo;
import com.ssm.song.book.mapper.CardMapper;
import com.ssm.song.book.mapper.ReaderMapper;
import com.ssm.song.book.service.IReaderService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author 宋伟宁
 * @Date 2023/10/18
 * @Version 1.0
 **/
@Service
public class ReaderService implements IReaderService {

       @Resource
        private ReaderMapper readerMapper;
       @Resource
       private CardMapper cardMapper;

    @Override
    public boolean register(Reader reader) {
         int m=0;
        // 插入数据到读者信息表
         m = this.readerMapper.insertSelective(reader);
        // 获取自增的主键id
        int userId = reader.getRid();
        // 随机的生成一个开号（卡的类型）
        Card card =new Card();
        card.setBalance(BigDecimal.valueOf(100.00));
        String num = "9211"+ new SimpleDateFormat("ddhhmmss").format(new Date());
        System.out.println("生成的卡号："+num);
        //生成卡号
        card.setCardnum(num);
        card.setCreatetime(new Date());
        card.setPassword("000000");
        //绑定读者
        card.setRid(userId);
        card.setTid(1);
        // 插入数据到借阅卡
        m += this.cardMapper.insertSelective(card);
        return m>=2;
    }

    @Override
    public ReaderVo login(String cardnum, String password) {
        return this.readerMapper.login(cardnum,password);
    }
}
